<template>
  <div class="page-search-list">
    <div class="page-search-list-filter">
      <el-input
          clearable
          placeholder="请输入"
          suffix-icon="el-icon-search"
          v-model="filter"
      />
    </div>
    <div v-if="$scopedSlots.header" class="page-search-list-header">
      <slot name="header"></slot>
    </div>
    <el-table
        size="mini"
        height="100%"
        highlight-current-row
        row-class-name="table-row-select"
        :data="filterData"
        :row-key="rowKey"
        :current-row-key="currentRowKey"
        v-loading="loading"
        @current-change="onCurrentChange"
    >
      <el-table-column label="公司名称" prop="label" show-overflow-tooltip min-width="80"/>
      <el-table-column label="编码" prop="code" show-overflow-tooltip min-width="80"/>

      <template v-slot:empty>
        <div class="table-empty">暂无数据</div>
      </template>
    </el-table>
    <div v-if="$scopedSlots.footer" class="page-search-list-footer">
      <slot name="footer"></slot>
    </div>
    <!--    <div class="page-search-list-footer">-->
    <!--      <el-button type="primary" icon="el-icon-plus">添加公司</el-button>-->
    <!--    </div>-->
  </div>
</template>

<script>
import axios from 'axios';

const CancelToken = axios.CancelToken;

export default {
  name: 'page-search-list',
  props: {
    value: null,
    url: {
      type: String,
      required: true,
    },
    params: Object,
    rowKey: {
      type: String,
      default: 'id',
    },
  },
  data() {
    return {
      filter: '',
      data: [],
      loading: false,
    };
  },
  computed: {
    currentRowKey() {
      if (!this.value) {
        return undefined;
      }
      return this.value[this.rowKey];
    },
    filterData() {
      if (!this.filter) {
        return this.data;
      }
      const filter = this.filter.toUpperCase();
      return this.data.filter(item => {
        return (item.name || '').toUpperCase().indexOf(filter) !== -1 //
            || (item.code || '').toUpperCase().indexOf(filter) !== -1;
      });
    },
  },
  watch: {
    url() {
      this.loadData();
    },
    params() {
      this.loadData();
    },
  },
  mounted() {
    this.init();
  },
  methods: {
    init() {
      this.loadData();
    },
    loadData() {
      if (this.source) {
        this.source.cancel();
      }
      const params = this.params || {};
      this.loading = true;
      this.source = CancelToken.source();
      this.$axios.get(this.url, {
        params,
        cancelToken: this.source.token,
      }).then((result) => {
        this.loading = false;
        const data = Array.isArray(result.data) ? result.data : [];
        this.setDefaultSelect(data);
        this.data = data;
      }).catch(() => {
        this.loading = false;
      });
    },
    setDefaultSelect(data) {
      if (data.length <= 0) {
        this.onChange();
        return;
      }
      const select = data.find(item => Object.is(item[this.valueKey], this.value));
      if (!select) {
        this.onChange(data[0]);
      }
    },
    onChange(value) {
      this.$emit('input', value);
      this.$emit('change', value);
    },
    onCurrentChange(currentRow) {
      this.onChange(currentRow);
    },
  },
};
</script>

<style lang="less">
.page-search-list {
  flex: 1;
  display: flex;
  flex-direction: column;
  background-color: #fff;

  .page-search-list-header {
    padding: 10px 15px;
    border-bottom: 1px solid #EBEEF5;
  }

  .page-search-list-footer {
    padding: 10px 15px;
  }

  .page-search-list-filter {
    padding: 15px;
    border-bottom: 1px solid #EBEEF5;

    .el-input {
      width: 100%;
    }
  }

  .page-search-list-footer {
    padding: 13px;

    .el-button {
      width: 100%;
    }

    .el-button + .el-button {
      margin-top: 10px;
    }
  }

  .el-table {
    flex: 1;

    .el-table__header-wrapper {
      display: none;
    }
  }

  .table-row-select {
    cursor: pointer;
  }

  .table-empty {
    padding-top: 110px;
    //background: url("../assets/img/empty.svg") center top no-repeat;
    background-size: auto 100px;
    color: rgba(0, 0, 0, 0.65);
    font-size: 14px;
    line-height: 22px;
    text-align: center;
  }
}
</style>
